<template>
  <div>
    <todo-header  @onAdd='onAdd' />
    <todo-main @delList='delList' v-for="item in list" :key="item.id" :completed='item.completed' :title='item.title' :id='item.id' />
    <!-- <todo-main :getList='list' @delList='delList' /> -->
    <todo-footer />
  </div>
</template>

<script>
import axios from 'axios'
import TodoFooter from './components/TodoFooter.vue'
import TodoHeader from './components/TodoHeader.vue'
import TodoMain from './components/TodoMain.vue'
axios.defaults.baseURL = 'http://liufusong.top:3001'
export default {
  data () {
    return {
      list: []
    }
  },
  components: { TodoHeader, TodoMain, TodoFooter },
  created () {
    this.getList()
  },

  methods: {
    async getList () {
      try {
        const res = await axios({
          url: '/list'
        })
        this.list = res.data
        console.log(this.list)
      } catch (err) {
        alert('请求失败')
      }
    },
    onAdd (val) {
      axios({
        url: '/add',
        method: 'POST',
        data: {
          title: val
        }
      }).then((res) => {
        console.log(res)
        this.getList()
      })
    },
    delList (val) {
      try {
        axios({
          url: '/list/' + val,
          method: 'DELETE'
        })
        this.getList()
      } catch (err) {
        alert('删除失败')
      }
    }
  }
}
</script>

<style scoped lang="less"></style>
